#include <QApplication>
#include <QTreeView>
#include <QTableView>
#include <QColumnView>
#include <QUndoView>
#include <QUndoStack>
#include <QUndoCommand>
#include <QStandardItemModel>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QWidget>
#include <QDebug>

// 自定义命令类：添加商品
class AddCommand : public QUndoCommand {
public:
    AddCommand(QStandardItemModel *model, const QList<QStandardItem *> &row, QUndoCommand *parent = nullptr)
            : QUndoCommand("添加商品", parent), m_model(model), m_row(row) {}

    void undo() override {
        m_model->removeRow(m_model->rowCount() - 1);
        qDebug() << "撤销添加商品";
    }

    void redo() override {
        m_model->appendRow(m_row);
        qDebug() << "执行添加商品";
    }

private:
    QStandardItemModel *m_model;
    QList<QStandardItem *> m_row;
};

// 自定义命令类：删除商品
class RemoveCommand : public QUndoCommand {
public:
    RemoveCommand(QStandardItemModel *model, int rowIndex, QUndoCommand *parent = nullptr)
            : QUndoCommand("删除商品", parent), m_model(model), m_rowIndex(rowIndex) {
        for (int i = 0; i < model->columnCount(); ++i)
            m_row.append(model->item(rowIndex, i)->clone());
    }

    void undo() override {
        m_model->insertRow(m_rowIndex, m_row);
        qDebug() << "撤销删除商品";
    }

    void redo() override {
        m_model->removeRow(m_rowIndex);
        qDebug() << "执行删除商品";
    }

private:
    QStandardItemModel *m_model;
    int m_rowIndex;
    QList<QStandardItem *> m_row;
};

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    // 主窗口和布局
    QWidget *mainWindow = new QWidget;
    mainWindow->setWindowTitle("综合视图管理系统");
    QVBoxLayout *mainLayout = new QVBoxLayout;
    QHBoxLayout *topLayout = new QHBoxLayout;

    // === 1. QTreeView：商品分类 ===
    QTreeView *treeView = new QTreeView;
    QStandardItemModel *treeModel = new QStandardItemModel;
    treeModel->setHorizontalHeaderLabels({"商品分类"});
    QStandardItem *root = treeModel->invisibleRootItem();
    QStandardItem *clothes = new QStandardItem("服装");
    clothes->appendRow(new QStandardItem("男装"));
    clothes->appendRow(new QStandardItem("女装"));
    root->appendRow(clothes);
    treeView->setModel(treeModel);
    treeView->expandAll();
    treeView->setFixedWidth(150);

    // === 2. QTableView：商品表格 ===
    QTableView *tableView = new QTableView;
    QStandardItemModel *tableModel = new QStandardItemModel;
    tableModel->setHorizontalHeaderLabels({"名称", "价格", "库存"});
    tableModel->appendRow({new QStandardItem("T恤"), new QStandardItem("99"), new QStandardItem("500")});
    tableView->setModel(tableModel);

    // === 3. QColumnView：商品品牌级联 ===
    QColumnView *columnView = new QColumnView;
    QStandardItemModel *columnModel = new QStandardItemModel;
    QStandardItem *brand1 = new QStandardItem("耐克");
    brand1->appendRow(new QStandardItem("运动鞋"));
    brand1->appendRow(new QStandardItem("夹克"));
    QStandardItem *brand2 = new QStandardItem("阿迪达斯");
    brand2->appendRow(new QStandardItem("卫衣"));
    columnModel->appendRow(brand1);
    columnModel->appendRow(brand2);
    columnView->setModel(columnModel);
    columnView->setFixedWidth(150);

    // === 4. QUndoView：操作历史 ===
    QUndoStack *undoStack = new QUndoStack;
    QUndoView *undoView = new QUndoView(undoStack);
    undoView->setWindowTitle("操作记录");
    undoView->setFixedHeight(120);

    // === 操作按钮 ===
    QPushButton *btnAdd = new QPushButton("添加商品");
    QPushButton *btnRemove = new QPushButton("删除商品");
    QPushButton *btnUndo = new QPushButton("撤销");
    QPushButton *btnRedo = new QPushButton("重做");
    QHBoxLayout *btnLayout = new QHBoxLayout;
    btnLayout->addWidget(btnAdd);
    btnLayout->addWidget(btnRemove);
    btnLayout->addWidget(btnUndo);
    btnLayout->addWidget(btnRedo);

    // === 布局组织 ===
    topLayout->addWidget(treeView);
    topLayout->addWidget(tableView, 1);
    topLayout->addWidget(columnView);
    mainLayout->addLayout(topLayout);
    mainLayout->addLayout(btnLayout);
    mainLayout->addWidget(undoView);
    mainWindow->setLayout(mainLayout);
    mainWindow->resize(800, 500);
    mainWindow->show();

    // === 连接按钮操作 ===
    QObject::connect(btnAdd, &QPushButton::clicked, [&]() {
        QList<QStandardItem *> row;
        row << new QStandardItem("新商品") << new QStandardItem("199") << new QStandardItem("100");
        undoStack->push(new AddCommand(tableModel, row));
    });

    QObject::connect(btnRemove, &QPushButton::clicked, [&]() {
        QModelIndex index = tableView->currentIndex();
        if (index.isValid()) {
            undoStack->push(new RemoveCommand(tableModel, index.row()));
        }
    });

    QObject::connect(btnUndo, &QPushButton::clicked, undoStack, &QUndoStack::undo);
    QObject::connect(btnRedo, &QPushButton::clicked, undoStack, &QUndoStack::redo);

    return app.exec();
}
